package bancosys.tec.persist.hibernate.type;

import java.sql.Clob;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.ClobType;

/**
 * @author lundberg
 */
public class UpdatableClobType extends ClobType {

    /**
     * Substitutes the original clob
     * 
     * @param original the value from the detached entity being merged
     * @param target the value in the managed entity
     * @param session session
     * @param owner owner
     * @param copyCache copyCache
     * @return the value to be merged
     */
    @Override
    public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache) {
        try {
            Clob clob = (Clob) original;
            return Hibernate.createClob(clob.getCharacterStream(), (int) clob.length());
        } catch (Exception e) {
            throw new HibernateException("Error copying the blob during the merge", e);
        }
    }
}
